package com.leo.springbootmultithreadbasic.singleton.lanhan;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Leo
 * @version V1.0
 */
public class MyObject {

    private static final Logger logger = LoggerFactory.getLogger(MyObject.class);


    private static MyObject myObject;

    private MyObject() {
    }

    public static MyObject getInstance() {

        // 最安全且最高效率的加锁方式：同步代码块&if双重判断
        if (null != myObject) {
            logger.debug("已创建实例");
        } else {
            synchronized (MyObject.class) {
                if (null == myObject) {
                    logger.info("开始创建实例");
                    myObject = new MyObject();
                } else {
                    logger.debug("测试双重判断生效");
                }

            }

        }


        return myObject;
    }
}
